package com.springdoor.web.controller.goods;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springdoor.common.annotation.Log;
import com.springdoor.common.core.controller.BaseController;
import com.springdoor.common.core.domain.AjaxResult;
import com.springdoor.common.enums.BusinessType;
import com.springdoor.common.core.domain.entity.SprdGoodsType;
import com.springdoor.goods.service.ISprdGoodsTypeService;
import com.springdoor.common.utils.poi.ExcelUtil;

/**
 * 商品类型Controller
 *
 * @author ruoyi
 * @date 2024-05-10
 */
@RestController
@RequestMapping("/goods/type")
public class SprdGoodsTypeController extends BaseController
{
    @Autowired
    private ISprdGoodsTypeService goodsTypeService;

    /**
     * 查询商品类型列表
     */
    @PreAuthorize("@ss.hasPermi('goods:type:list')")
    @GetMapping("/list")
    public AjaxResult list(SprdGoodsType sprdGoodsType)
    {
        List<SprdGoodsType> list = goodsTypeService.selectSprdGoodsTypeList(sprdGoodsType);
        return success(list);
    }

    /**
     * 导出商品类型列表
     */
    @PreAuthorize("@ss.hasPermi('goods:type:export')")
    @Log(title = "商品类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SprdGoodsType sprdGoodsType)
    {
        List<SprdGoodsType> list = goodsTypeService.selectSprdGoodsTypeList(sprdGoodsType);
        ExcelUtil<SprdGoodsType> util = new ExcelUtil<SprdGoodsType>(SprdGoodsType.class);
        util.exportExcel(response, list, "商品类型数据");
    }

    /**
     * 获取商品类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:type:query')")
    @GetMapping(value = "/{gTypeId}")
    public AjaxResult getInfo(@PathVariable("gTypeId") Long gTypeId)
    {
        return success(goodsTypeService.selectSprdGoodsTypeByGTypeId(gTypeId));
    }

    /**
     * 新增商品类型
     */
    @PreAuthorize("@ss.hasPermi('goods:type:add')")
    @Log(title = "商品类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SprdGoodsType goodsType)
    {
        if (!goodsTypeService.checkGoodsTypeNameUnique(goodsType))
        {
            return error("新增商品类型失败，商品类型名称" + goodsType.getgTypeName() + "已存在");
        }
        goodsType.setCreateBy(getUsername());
        goodsType.setUpdateBy(getUsername());
        return toAjax(goodsTypeService.insertSprdGoodsType(goodsType));
    }

    /**
     * 修改商品类型
     */
    @PreAuthorize("@ss.hasPermi('goods:type:edit')")
    @Log(title = "商品类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SprdGoodsType goodsType)
    {
        if (!goodsTypeService.checkGoodsTypeNameUnique(goodsType))
        {
            return error("修改商品类型失败，商品类型名称" + goodsType.getgTypeName() + "已存在");
        }
        goodsType.setUpdateBy(getUsername());
        return toAjax(goodsTypeService.updateSprdGoodsType(goodsType));
    }

    /**
     * 修改商品类型
     */
    @PreAuthorize("@ss.hasPermi('goods:type:edit')")
    @Log(title = "商品分类=>状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("updateStatus")
    public AjaxResult updateStatus(@RequestBody SprdGoodsType goodsType)
    {
        SprdGoodsType type = new SprdGoodsType();
        type.setgTypeId(goodsType.getgTypeId());
        type.setDelFlag(goodsType.getDelFlag());
        type.setHomeShow(goodsType.getHomeShow());
        type.setUpdateBy(getUsername());
        return toAjax(goodsTypeService.updateTypeStatus(type));
    }

    @PreAuthorize("@ss.hasPermi('goods:type:edit')")
    @GetMapping("/findMaxOrderNum/{typeId}")
    public AjaxResult findMaxOrderNum(@PathVariable Long typeId) {
        return success(goodsTypeService.findMaxOrderNum(typeId));
    }

    @GetMapping("/optionSelect")
    public AjaxResult optionSelect() {
        return success(goodsTypeService.selectTypeAll());
    }

    @GetMapping("/treeSelect")
    public AjaxResult treeSelect() {
        List<SprdGoodsType> list = goodsTypeService.selectTypeAll();
        return success(goodsTypeService.buildAttrTreeSelect(list));
    }
}
